package com.hydra.auth.controller;

import com.hydra.auth.service.LoginService;
import com.hydra.base.annotations.ControllerLogDisabled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@Controller
@RequestMapping("/api/kaptcha")
public class KaptchaController {

    @Resource
    private LoginService loginService;

    /**
     * 获取图形验证码接口
     * @param key
     */
    @ControllerLogDisabled
    @GetMapping(value = "/image.jpg")
    public void getValidateCode(@RequestParam("verifyCodeKey") String key, HttpServletResponse response) throws Exception{
        response.setDateHeader("Expires", 0L);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");

        BufferedImage image = loginService.getValidateCode(key);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }

}
